const express = require("express");
const app = express();

const session = require("express-session");

const cors = require("cors");

/**
 * 中间件
 */
app.use(cors());
//app.use(cors({
// origin: 'http://localhost:5713',
///允许跨域访问 ,这一步非常重要，否则前端无法访问到后端的数据
//credentials: true //允许携带cookie  ，前端已经设置了withCredentials: true，后端需要带上credentials: true
//由于前端加了withCredentials: true, origin必须设置，否则计算挂载了cors，也会有跨域错误
//    credentials: true
//}))
app.use(express.json());
app.use(express.urlencoded({ extended: true })); //解析post请求提交的表单
app.use(
  session({
    secret: "secret-key-enlist",
    resave: false,
    saveUninitialized: true,
  })
);
/**
 * 用户路由
 */
const loginRouter = require("./routers/login.js");
const saleRouter = require("./routers/sale.js");
const distributeRouter = require("./routers/distribute.js");
const povertyAchievementRouter = require("./routers/povertyAchievement.js");
const uploadImgRouter = require("./routers/uploadImg.js");
const policyRouter = require("./routers/policy.js");
const productRouter = require("./routers/product.js");
const productImgRouter = require("./routers/productImg.js");
const povertyReasonsRouter = require("./routers/povertyReasons.js");

/** 路由挂载*/
app.use('/uploads', express.static('uploads'));//挂载静态路径否则将无法使用到图片

app.use("/api", loginRouter);
app.use("/api", saleRouter);
app.use("/api", distributeRouter);
app.use("/api", povertyAchievementRouter);
app.use("/api", uploadImgRouter);
app.use("/api", policyRouter);
app.use("/api", productRouter);
app.use("/api", productImgRouter);
app.use("/api", povertyReasonsRouter);

app.listen(3001, () => {
  console.log("index on port 3001，http://127.0.0.1:3001");
});
